package com.qfmy.mystation.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.qfmy.mystation.R;
import com.qfmy.mystation.bean.Wallpaper;

import java.util.ArrayList;

public class MyWallpaperAdapter extends RecyclerView.Adapter<MyWallpaperAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Wallpaper> wallpapers;

    public MyWallpaperAdapter(Context context, ArrayList<Wallpaper> wallpapers) {
        this.context = context;
        this.wallpapers = wallpapers;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_img, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Wallpaper bean = wallpapers.get(position);
        Glide.with(context)
                .load(bean.getUrl())
                .into(holder.iv_item_img);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.rv_tag.setLayoutManager(linearLayoutManager);
        holder.rv_tag.setAdapter(new TagListAdapter(bean.getTag().split(","), R.layout.item_tag));
    }

    @Override
    public int getItemCount() {
        return wallpapers == null ? 0 : wallpapers.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_item_img;
        RecyclerView rv_tag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_item_img = itemView.findViewById(R.id.iv_item_img);
            rv_tag = itemView.findViewById(R.id.rv_tag);
        }
    }
}
